
const app = getApp()
const util = require('../../utils/util.js')
import {Date2} from '../../utils/date2.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    family: 0, //所在家族
    target_person: 0, //添加谁的丈夫
    target_person_name: '', 

    select_family: 0, //当前选择家族，取0表示新增人员
    select_person: 0, //当前选择人的id, 如果取0表示新增人员
    priority: 0,      //当前固定排行
    priority_chn: '', //排行的中文名

    //编辑信息
    input_name: '',
    input_nick: '',
    input_birthday: '',  //数据库格式
    input_birthday_show: '', //展示格式
    input_phone: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      family: parseInt(options.family),
      target_person: parseInt(options.target_person),
      target_person_name: decodeURI(options.target_person_name),
      priority: parseInt(options.priority),
      priority_chn: util.priority2Chn(parseInt(options.priority))
    })
    if (options.select_family && options.select_person) {
      this.setData({
        select_family: parseInt(options.select_family),
        select_person: parseInt(options.select_person)
      })
    }

    wx.setNavigationBarTitle({
      title: `添加${this.data.target_person_name}的丈夫`,
    })

    if (this.data.select_family > 0 && this.data.select_person > 0) {
      //有选择人 , 获取初始数据
      app.httpRequest('get_relation_person_info_add', {
        select_family: this.data.select_family,
        select_person: this.data.select_person,
        parent_couple: 0,
        family: this.data.family
      }, res => {
        console.log('get_relation_person_info_add succ: ', res)

        this.setData({
          input_name: res.info.name,
          input_nick: res.info.nick,
          input_birthday: res.info.birthday,
          input_phone: res.info.phone,
        })
        let date2 = new Date2()
        let ok = date2.ParseDbDateStr(res.info.birthday)
        if (ok) {
          this.setData({
            input_birthday_show: date2.FormatShowDate()
          })
        }

        console.log("添加丈夫页初始化数据：", this.data)
      }, res => {
        console.log('get_relation_person_info_add failed: ', res)
      })
    } else {
      //新增人
      console.log("添加丈夫页初始化数据：", this.data)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindNameInput(e) {
    this.setData({
      input_name: e.detail.value
    })
  },

  bindSexInput(e) {
    this.setData({
      input_sex: e.detail.value
    })
  },

  bindNickInput(e) {
    this.setData({
      input_nick: e.detail.value
    })
  },

  bindPhoneInput(e) {
    this.setData({
      input_phone: e.detail.value
    })
  },

  bindTapBirthday(e) {
    console.log("click birthday")
    //传入数据库时间字符串

    let dbDateStr = ''
    wx.navigateTo({
      url: '../family/edit_date?date=' + dbDateStr,
    })
  },

  editDateCallback(date2) {
    this.setData({
      input_birthday: date2.FormatDbDateStr(),
      input_birthday_show: date2.FormatShowDate()
    })
  },

  bindTapBack() {
    wx.navigateBack({
      delta: 0,
    })
  },

  bindTapSave() {
    if (this.data.priority == 0) {
      app.showTip('排行信息异常，不能保存')
      return
    }
    let name = this.data.input_name.trim()
    if (name.length == 0) {
      app.showTip('姓名不能为空')
      return
    }
    let nick = this.data.input_nick.trim()
    let phone = this.data.input_phone.trim()
    let birthday = this.data.input_birthday

    //调用保存接口
    app.httpRequest('family_add_husband', {
      family: this.data.family,
      target_person: this.data.target_person,
      select_person: this.data.select_person,
      name: name,
      nick: nick,
      birthday: birthday,
      phone: phone,
      priority: this.data.priority
    }, res => {
      console.log('family_add_husband succ: ', res)

      //返回
      let pages = getCurrentPages()  //获取当前页面js里面的pages里的所有信息。
      let prevPage = pages[pages.length - 2]  //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。
      prevPage.refreshPersons(res)
      
      wx.navigateBack()

    }, res => {
      console.log('family_add_husband failed: ', res)
    })

    
  }
})